system-controller scripting: changes to adapt to new HS protocol
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 3
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 -- PulseAudio volume context
239 mdb.table {
240     name = "volume_context",
241     index = { "id" },
242     create = true,
243     columns = {
244         { "id", mdb.unsigned },
245         { "value", mdb.string, 64 },
246     }
247 }
248
249 -- put default volume context to the table
250 mdb.table.volume_context:insert({ id = 1, value = "default" })
251
252 if not m:plugin_exists('ivi-resource-manager') and
253    not with_system_controller
254 then
255     resource.method.veto = {
256         function(zone, rset, grant, owners, req_set)
257             return true
258          end
259     }
260 end
261
262 -- test for creating selections
263 mdb.select {
264            name = "audio_owner",
265            table = "audio_playback_owner",
266            columns = {"application_class"},
267            condition = "zone_name = 'driver'"
268 }
269
270 mdb.select {
271            name = "vehicle_speed",
272            table = "amb_vehicle_speed",
273            columns = {"value"},
274            condition = "key = 'VehicleSpeed'"
275 }
276
277 element.lua {
278    name    = "speed2volume",
279    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
280    outputs = {  mdb.table { name = "speedvol",
281                             index = {"zone", "device"},
282                             columns = {{"zone", mdb.string, 16},
283                                        {"device", mdb.string, 16},
284                                        {"value", mdb.floating}},
285                             create = true
286                            }
287              },
288    oldvolume = 0.0,
289    update  = function(self)
290                 speed = self.inputs.speed.single_value
291                 if (speed) then
292                     volume = (speed - 144.0) / 7.0
293                 else
294                     volume = 0.0
295                 end
296                 diff = volume - self.oldvolume
297                 if (diff*diff > self.inputs.param) then
298                     print("*** element "..self.name.." update "..volume)
299                     self.oldvolume = volume
300                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
301                 end
302              end
303 }
304
305 mdb.select {
306     name = "amb_state",
307     table = "amb_state",
308     columns = { "state" },
309     condition = "id = 0"
310 }
311
312 -- Night mode processing chain
313
314 mdb.select {
315     name = "exterior_brightness",
316     table = "amb_exterior_brightness",
317     columns = { "value" },
318     condition = "key = 'ExteriorBrightness'"
319 }
320
321 element.lua {
322     name    = "nightmode",
323     inputs  = { brightness = mdb.select.exterior_brightness },
324     oldmode = -1;
325     outputs = {
326         mdb.table {
327             name = "amb_nightmode",
328             index = { "id" },
329             create = true,
330             columns = {
331                 { "id", mdb.unsigned },
332                 { "night_mode", mdb.unsigned }
333             }
334         }
335     },
336     update = function(self)
337         -- This is a trivial function to calculate night mode. Later, we will
338         -- need a better threshold value and hysteresis to prevent oscillation.
339
340         brightness = self.inputs.brightness.single_value
341
342         if not brightness then
343             return
344         end
345
346         print("*** element "..self.name.." update brightness: "..brightness)
347
348         if brightness > 300 then
349             mode = 0
350         else
351             mode = 1
352         end
353
354         print("*** resulting mode: ".. mode)
355
356         if not (mode == self.oldmode) then
357             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
358         end
359
360         self.oldmode = mode
361     end
362 }
363
364 mdb.select {
365     name = "select_night_mode",
366     table = "amb_nightmode",
367     columns = { "night_mode" },
368     condition = "id = 0"
369 }
370
371 if with_amb then
372     sink.lua {
373         name = "night_mode",
374         inputs = { NightMode = mdb.select.select_night_mode,
375                    amb_state = mdb.select.amb_state },
376         property = "NightMode",
377         type = "b",
378         initiate = builtin.method.amb_initiate,
379         update = builtin.method.amb_update
380     }
381 end
382
383 -- Night mode general handlers
384
385 if with_system_controller then
386     sink.lua {
387         name = "nightmode_homescreen",
388         inputs = { owner = mdb.select.select_night_mode },
389         initiate = function(self)
390                 -- data = mdb.select.select_night_mode.single_value
391                 return true
392             end,
393         update = function(self)
394                 send_night_mode_to_home_screen()
395             end
396     }
397 end
398
399 -- Driving mode processing chain
400
401 element.lua {
402     name    = "drivingmode",
403     inputs  = { speed = mdb.select.vehicle_speed },
404     oldmode = -1;
405     outputs = {
406         mdb.table {
407             name = "amb_drivingmode",
408             index = { "id" },
409             create = true,
410             columns = {
411                 { "id", mdb.unsigned },
412                 { "driving_mode", mdb.unsigned }
413             }
414         }
415     },
416     update = function(self)
417
418         speed = self.inputs.speed.single_value
419
420         if not speed then
421             return
422         end
423
424         if speed == 0 then
425             mode = 0
426         else
427             mode = 1
428         end
429
430         if not (mode == self.oldmode) then
431             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
432         end
433
434         self.oldmode = mode
435     end
436 }
437
438 mdb.select {
439     name = "select_driving_mode",
440     table = "amb_drivingmode",
441     columns = { "driving_mode" },
442     condition = "id = 0"
443 }
444
445 if with_amb then
446     sink.lua {
447         name = "driving_mode",
448         inputs = { DrivingMode = mdb.select.select_driving_mode,
449                    amb_state = mdb.select.amb_state },
450         property = "DrivingMode",
451         type = "u",
452         initiate = builtin.method.amb_initiate,
453         update = builtin.method.amb_update
454     }
455 end
456
457 -- turn signals (left, right)
458
459 mdb.select {
460     name = "winker",
461     table = "amb_turn_signal",
462     columns = { "value" },
463     condition = "key = 'TurnSignal'"
464 }
465
466 -- define three categories
467
468 mdb.select {
469     name = "undefined_applications",
470     table = "aul_applications",
471     columns = { "appid" },
472     condition = "category = '<undefined>'"
473 }
474
475 mdb.select {
476     name = "basic_applications",
477     table = "aul_applications",
478     columns = { "appid" },
479     condition = "category = 'basic'"
480 }
481
482 mdb.select {
483     name = "entertainment_applications",
484     table = "aul_applications",
485     columns = { "appid" },
486     condition = "category = 'entertainment'"
487 }
488
489 function ft(t)
490     -- filter the object garbage out of the tables
491     ret = {}
492
493     for k,v in pairs(t) do
494         if k ~= "userdata" and k ~= "new" then
495             ret[k] = v
496         end
497     end
498
499     return ret
500 end
501
502 function getApplication(appid)
503     local conf = nil
504
505     -- find the correct local application definition
506
507     for k,v in pairs(ft(application)) do
508         if appid == v.appid then
509             conf = v
510             break
511         end
512     end
513
514     return conf
515 end
516
517 function regulateApplications(t, regulation)
518     for k,v in pairs(ft(t)) do
519
520         -- iterate through the undefined and entertainment apps, see if
521         -- they have been overruled in local config
522
523         local conf = getApplication(v.appid)
524
525         if conf and conf.resource_class ~= "player" then
526             -- override, don't disable
527             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
528         else
529             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
530         end
531     end
532     resource.method.recalc("driver")
533 end
534
535 -- regulation (on), use "select_driving_mode"
536
537 sink.lua {
538     name = "driving_regulation",
539     inputs = { owner = mdb.select.select_driving_mode },
540     initiate = function(self)
541         -- local data = mdb.select.select_driving_mode.single_value
542         return true
543     end,
544     update = function(self)
545         local data = mdb.select.select_driving_mode.single_value
546
547         if verbose > 1 then
548             print("Driving mode updated: " .. tostring(data))
549         end
550
551         if not sc then
552             return true
553         end
554
555         -- tell homescreen that driving mode was updated
556         send_driving_mode_to_home_screen()
557
558         regulateApplications(ft(mdb.select.entertainment_applications), data)
559         regulateApplications(ft(mdb.select.undefined_applications), data)
560
561         return true
562     end
563 }
564 --[[
565 sink.lua {
566     name = "regulated_app_change",
567     inputs = { undef = mdb.select.undefined_applications,
568                entertainment = mdb.select.entertainment_applications },
569     initiate = function(self)
570         return true
571     end,
572     update = function(self)
573         local data = mdb.select.select_driving_mode.single_value
574
575         if not sc then
576             return
577         end
578
579         if verbose > 1 then
580             print("regulated application list was changed")
581         end
582
583         regulateApplications(ft(mdb.select.entertainment_applications), data)
584         regulateApplications(ft(mdb.select.undefined_applications), data)
585
586         return true
587     end
588 }
589 --]]
590
591 -- shift position (parking, reverse, other)
592
593 mdb.select {
594     name = "gear_position",
595     table = "amb_gear_position",
596     columns = { "value" },
597     condition = "key = 'GearPosition'"
598 }
599
600 -- cameras (back, front, left, right)
601
602 element.lua {
603     name    = "camera_state",
604     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
605     oldmode = -1;
606     outputs = {
607         mdb.table {
608             name = "target_camera_state",
609             index = { "id" },
610             create = true,
611             columns = {
612                 { "id", mdb.unsigned },
613                 { "front_camera", mdb.unsigned },
614                 { "back_camera", mdb.unsigned },
615                 { "right_camera", mdb.unsigned },
616                 { "left_camera", mdb.unsigned }
617             }
618         }
619     },
620     update = function(self)
621
622         front_camera = 0
623         back_camera = 0
624         right_camera = 0
625         left_camera = 0
626
627         if self.inputs.gear == 128 then
628             back_camera = 1
629         elseif self.inputs.winker == 1 then
630             right_camera = 1
631         elseif self.inputs.winker == 2 then
632             left_camera = 1
633         end
634
635         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
636
637     end
638 }
639
640 -- system controller test setup
641
642 if not with_system_controller then
643    -- ok, we should have 'audio_playback' defined by now
644    m:try_load_plugin('telephony')
645    return
646 end
647
648 m:load_plugin('system-controller')
649
650 window_manager_operation_names = {
651     [1] = "create",
652     [2] = "destroy"
653 }
654
655 function window_manager_operation_name(oper)
656     local name = window_manager_operation_names[oper]
657     if name then return name end
658     return "<unknown " .. tostring(oper) .. ">"
659 end
660
661 window_operation_names = {
662     [1] = "create",
663     [2] = "destroy",
664     [3] = "name_change",
665     [4] = "visible",
666     [5] = "configure",
667     [6] = "active",
668     [7] = "map",
669     [8] = "hint"
670 }
671
672 function window_operation_name(oper)
673     local name = window_operation_names[oper]
674     if name then return name end
675     return "<unknown " .. tostring(oper) .. ">"
676 end
677
678 layer_operation_names = {
679     [1] = "create",
680     [2] = "destroy",
681     [3] = "visible"
682 }
683
684 function layer_operation_name(oper)
685     local name = layer_operation_names[oper]
686     if name then return name end
687     return "<unknown " .. tostring(oper) .. ">"
688 end
689
690 input_manager_operation_names = {
691     [1] = "create",
692     [2] = "destroy",
693     [3] = "ready"
694 }
695
696 function input_manager_operation_name(oper)
697     local name = input_manager_operation_names[oper]
698     if name then return name end
699     return "<unknown " .. tostring(oper) .. ">"
700 end
701
702 input_operation_names = {
703     [1] = "create",
704     [2] = "destroy",
705     [3] = "update"
706 }
707
708 function input_operation_name(oper)
709     local name = input_operation_names[oper]
710     if name then return name end
711     return "<unknown " .. tostring(oper) .. ">"
712 end
713
714 code_operation_names = {
715     [1] = "create",
716     [2] = "destroy",
717     [3] = "state_change"
718 }
719
720 function code_operation_name(oper)
721     local name = code_operation_names[oper]
722     if name then return name end
723     return "<unknown " .. tostring(oper) .. ">"
724 end
725
726 command_names = {
727     [0x00001] = "send_appid",
728     [0x10001] = "create",
729     [0x10002] = "destroy",
730     [0x10003] = "show",
731     [0x10004] = "hide",
732     [0x10005] = "move",
733     [0x10006] = "animation",
734     [0x10007] = "change_active",
735     [0x10008] = "change_layer",
736     [0x10009] = "change_attr",
737     [0x10010] = "name",
738     [0x10020] = "map_thumb",
739     [0x10021] = "unmap_thumb",
740     [0x10022] = "map_get",
741     [0x10030] = "show layer",
742     [0x10031] = "hide_layer",
743     [0x10032] = "change_layer_attr",
744     [0x20001] = "add_input",
745     [0x20002] = "del_input",
746     [0x30001] = "change_user",
747     [0x30002] = "get_userlist",
748     [0x30003] = "get_lastinfo",
749     [0x30004] = "set_lastinfo",
750     [0x40001] = "acquire_res",
751     [0x40002] = "release_res",
752     [0x40003] = "deprive_res",
753     [0x40004] = "waiting_res",
754     [0x40005] = "revert_res",
755     [0x40006] = "window_id_res",
756     [0x40011] = "create_res",
757     [0x40012] = "destroy_res",
758     [0x50001] = "set_region",
759     [0x50002] = "unset_region",
760     [0x60001] = "change_state"
761 }
762
763 function command_name(command)
764     local name = command_names[command]
765     if name then return name end
766     return "<unknown " .. tostring(command) .. ">"
767 end
768
769 input_layer = {
770    [101] = true, -- input
771    [102] = true, -- touch
772    [103] = true  -- cursor
773 }
774
775 -- some day this should be merged with wmgr.layers
776 ico_layer_type = {
777    [1]   = 0x1000, -- background
778    [2]   = 0x2000, -- application
779    [3]   = 0x2000, -- homescreen
780    [4]   = 0x2000, -- interrupt application
781    [5]   = 0x2000, -- onscreen application
782    [6]   = 0xc000, -- startup
783    [7]   = 0x3000, -- fullscreen
784    [101] = 0x4000, -- input
785    [102] = 0xa000, -- touch
786    [103] = 0xb000  -- cursor
787 }
788
789 resmgr = resource_manager {
790   screen_event_handler = function(self, ev)
791                              local event = ev.event
792                              local surface = ev.surface
793
794                              if event == "init" then
795                                  if verbose > 0 then
796                                      print("*** init screen resource allocation -- disable all 'player'")
797                                  end
798                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
799                              elseif event == "preallocate" then
800                                  if verbose > 0 then
801                                      print("*** preallocate screen resource "..
802                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
803                                  end
804                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
805                              elseif event == "grant" then
806                                  if verbose > 0 then
807                                     print("*** make visible surface "..surface)
808                                  end
809                                  local a = animation({})
810                                  local r = m:JSON({surface = surface,
811                                                    visible = 1,
812                                                    raise   = 1})
813                                  wmgr:window_request(r,a,0)
814                              elseif event == "revoke" then
815                                  if verbose > 0 then
816                                     print("*** hide surface "..surface)
817                                  end
818                                  local a = animation({})
819                                  local r = m:JSON({surface = ev.surface,
820                                                    visible = 0})
821                                  wmgr:window_request(r,a,0)
822
823                              elseif event == "create" then
824
825                                 if verbose > 0 then
826                                     print("*** screen resource event: " ..
827                                           tostring(ev))
828                                 end
829
830                                 local regulation = mdb.select.select_driving_mode.single_value
831
832                                 if regulation == 1 then
833
834                                     local blacklisted = false
835
836                                     -- applications which have their category set to "entertainment"
837                                     -- or "undefined" are blacklisted, meaning they should be regulated
838
839                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
840                                         if v.appid == ev.appid then
841                                             if verbose > 0 then
842                                                 print(ev.appid .. " was blacklisted (undefined)")
843                                             end
844                                             blacklisted = true
845                                             break
846                                         end
847                                     end
848
849                                     if not blacklisted then
850                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
851                                             if v.appid == ev.appid then
852                                                 if verbose > 0 then
853                                                     print(ev.appid .. " was blacklisted (entertainment)")
854                                                 end
855                                                 blacklisted = true
856                                                 break
857                                             end
858                                         end
859                                     end
860
861                                     -- our local application config, which takes precedence
862                                     local conf = getApplication(ev.appid)
863
864                                     -- disable only non-whitelisted applications
865                                     if not conf or conf.resource_class == "player" then
866                                         if blacklisted then
867                                             if verbose > 0 then
868                                                 print("disabling screen for " .. ev.appid)
869                                             end
870                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
871                                         end
872                                     end
873                                 end
874
875                              elseif event == "destroy" then
876                                if verbose > 0 then
877                                     print("*** screen resource event: " ..
878                                           tostring(ev))
879                                  end
880                              else
881                                  if verbose > 0 then
882                                     print("*** screen resource event: " ..
883                                           tostring(ev))
884                                  end
885                              end
886                          end,
887   audio_event_handler = function(self, ev)
888                              local event = ev.event
889                              local appid = ev.appid
890                              local audioid = ev.audioid
891
892                              if event == "grant" then
893                                  if verbose > 0 then
894                                     print("*** grant audio to "..appid..
895                                           " ("..audioid..") in '" ..
896                                           ev.zone .. "' zone")
897                                  end
898                              elseif event == "revoke" then
899                                  if verbose > 0 then
900                                     print("*** revoke audio from "..appid..
901                                           " ("..audioid..") in '" ..
902                                           ev.zone .. "' zone")
903                                  end
904                              else
905                                  if verbose > 0 then
906                                     print("*** audio resource event: " ..
907                                           tostring(ev))
908                                  end
909                              end
910                         end
911 }
912
913 resclnt = resource_client {}
914
915 wmgr = window_manager {
916   geometry = function(self, w,h, v)
917                   if type(v) == "function" then
918                       return v(w,h)
919                   end
920                   return v
921              end,
922
923   application = function(self, appid)
924                      if appid then
925                          local app = application_lookup(appid)
926                          if not app then
927                              app = application_lookup("default")
928                          end
929                          return app
930                      end
931                      return { privileges = {screen="none", audio="none"} }
932                 end,
933
934   output_order = { 1, 0 },
935
936   outputs = { { name  = "Mid",
937                 id    = 1,
938                 zone  = "driver",
939                 areas = { Full = {
940                               id     = 20,
941                               pos_x  = 0,
942                               pos_y  = 0,
943                               width  = function(w,h) return w end,
944                               height = function(w,h) return h end
945                           },
946                           Left = {
947                               id     = 21,
948                               pos_x  = 0,
949                               pos_y  = 0,
950                               width  = 320,
951                               height = function(w,h) return h end
952                           },
953                           Right = {
954                               id     = 22,
955                               pos_x  = function(w,h) return w-320 end,
956                               pos_y  = 0,
957                               width  = 320,
958                               height = function(w,h) return h end
959                           }
960                         }
961                },
962                { name  = "Center",
963                  id    = 4,
964                  zone  = "driver",
965                  areas = { Status = {
966                               id     = 0,
967                               pos_x  = 0,
968                               pos_y  = 0,
969                               width  = function(w,h) return w end,
970                               height = 64
971                            },
972                            Full = {
973                               id     = 1,
974                               pos_x  = 0,
975                               pos_y  = 64,
976                               width  = function(w,h) return w end,
977                               height = function(w,h) return h-64-128 end
978                            },
979                            Upper = {
980                               id     = 2,
981                               pos_x  = 0,
982                               pos_y  = 64,
983                               width  = function(w,h) return w end,
984                               height = function(w,h) return (h-64-128)/2 end
985                            },
986                            Lower = {
987                               id     = 3,
988                               pos_x  = 0,
989                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
990                               width  = function(w,h) return w end,
991                               height = function(w,h) return (h-64-128)/2 end
992                            },
993                            UpperLeft = {
994                               id     = 4,
995                               pos_x  = 0,
996                               pos_y  = 64,
997                               width  = function(w,h) return w/2 end,
998                               height = function(w,h) return (h-64-128)/2 end
999                            },
1000                            UpperRight = {
1001                               id     = 5,
1002                               pos_x  = function(w,h) return w/2 end,
1003                               pos_y  = 64,
1004                               width  = function(w,h) return w/2 end,
1005                               height = function(w,h) return (h-64-128)/2 end
1006                            },
1007                            LowerLeft = {
1008                               id     = 6,
1009                               pos_x  = 0,
1010                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1011                               width  = function(w,h) return w/2 end,
1012                               height = function(w,h) return (h-64-128)/2 end
1013                            },
1014                            LowerRight = {
1015                               id     = 7,
1016                               pos_x  = function(w,h) return w/2 end,
1017                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1018                               width  = function(w,h) return w/2 end,
1019                               height = function(w,h) return (h-64-128)/2 end
1020                            },
1021                            SysApp = {
1022                               id     = 8,
1023                               pos_x  = 0,
1024                               pos_y  = 64,
1025                               width  = function(w,h) return w end,
1026                               height = function(w,h) return h-64-128 end
1027                            },
1028                            ["SysApp.Left"] = {
1029                               id     = 9,
1030                               pos_x  = 0,
1031                               pos_y  = 64,
1032                               width  = function(w,h) return w/2-181 end,
1033                               height = function(w,h) return h-64-128 end
1034                            },
1035                            ["SysApp.Right"] = {
1036                               id     = 10,
1037                               pos_x  = function(w,h) return w/2+181 end,
1038                               pos_y  = 64,
1039                               width  = function(w,h) return w/2-181 end,
1040                               height = function(w,h) return h-64-128 end
1041                            },
1042                            MobileFull = {
1043                               id     = 11,
1044                               pos_x  = 0,
1045                               pos_y  = 64,
1046                               width  = function(w,h) return w end,
1047                               height = function(w,h) return h-64-128 end
1048                            },
1049                            MobileUpper = {
1050                               id     = 12,
1051                               pos_x  = 0,
1052                               pos_y  = 64,
1053                               width  = function(w,h) return w end,
1054                               height = function(w,h) return (h-64-128)/2 end
1055                            },
1056                            MobileLower = {
1057                               id     = 13,
1058                               pos_x  = 0,
1059                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1060                               width  = function(w,h) return w end,
1061                               height = function(w,h) return (h-64-128)/2 end
1062                            },                           
1063                            Control = {
1064                               id     = 14,
1065                               pos_x  = 0,
1066                               pos_y  = function(w,h) return h-128 end,
1067                               width  = function(w,h) return w end,
1068                               height = 128
1069                            },
1070                         }
1071               }
1072   },
1073              --    id   name            type  output
1074   layers = { {      0, "BackGround"   ,    1, "Center" },
1075              {      1, "Application"  ,    2, "Center" },
1076              {      2, "HomeScreen"   ,    3, "Center" },
1077              {      3, "ControlBar"   ,    3, "Center" },
1078              {      4, "InterruptApp" ,    4, "Center" },
1079              {      5, "OnScreen"     ,    5, "Center" },
1080              {      6, "Touch"        ,  102, "Center" },
1081              {      7, "Cursor"       ,  103, "Center" }
1082   },
1083
1084
1085   manager_update = function(self, oper)
1086                        if verbose > 0 then
1087                            print("### <== WINDOW MANAGER UPDATE:" ..
1088                                  window_manager_operation_name(oper))
1089                        end
1090                        if oper == 1 then
1091                            local wumask = window_mask { -- raise   = true,
1092                                                         visible = true,
1093                                                         active  = true }
1094                            local wrmask = window_mask { raise   = true,
1095                                                         active  = true,
1096                                                         layer   = true }
1097                            local lumask = layer_mask  { visible = true }
1098                            local lrmask = layer_mask  { visible = true }
1099                            local req = m:JSON({
1100                                passthrough_window_update  = wumask:tointeger(),
1101                                passthrough_window_request = wrmask:tointeger(),
1102                                passthrough_layer_update   = lumask:tointeger(),
1103                                passthrough_layer_request  = lrmask:tointeger()
1104                            })
1105                            self:manager_request(req)
1106                        end
1107                    end,
1108
1109   window_update = function(self, oper, win, mask)
1110                       if verbose > 0 then
1111                           print("### <== WINDOW UPDATE oper:" ..
1112                                 window_operation_name(oper) ..
1113                                 " mask: " .. tostring(mask))
1114                           if verbose > 1 then
1115                               print(win)
1116                           end
1117                       end
1118
1119                       local arg = m:JSON({ surface = win.surface,
1120                                            winname = win.name,
1121                       })
1122                       local command = 0
1123
1124                       if oper == 1 then -- create
1125                            local layertype = win.layertype
1126                            if layertype and input_layer[layertype] then
1127                                if verbose > 0 then
1128                                    print("ignoring input panel creation")
1129                                end
1130                                return
1131                            end
1132                            command     = 0x10001
1133                       elseif oper == 2 then -- destroy
1134                            command     = 0x10002
1135                       elseif oper == 3 then  -- namechange
1136                            command     = 0x10010
1137                       elseif oper == 4 or oper == 5 then -- visible/configure
1138                            command     = 0x10009
1139                            arg.zone    = win.area
1140                            arg.node    = win.node
1141                            if win.layertype then
1142                                arg.layertype = win.layertype
1143                            end
1144                            arg.layer   = win.layer
1145                            arg.pos_x   = win.pos_x
1146                            arg.pos_y   = win.pos_y
1147                            arg.width   = win.width
1148                            arg.height  = win.height
1149                            arg.raise   = win.raise
1150                            arg.visible = win.visible
1151                            if win.active == 0 then
1152                                arg.active = 0
1153                            else
1154                                arg.active = 1
1155                            end
1156                       elseif oper == 6 then -- active
1157                            if win.active == 0 then
1158                                if verbose > 0 then
1159                                    print("ignoring inactive event")
1160                                end
1161                                return
1162                            end
1163                            command = 0x10007
1164                       elseif oper == 7 then -- map
1165                            local map = win.map
1166                            if not map then
1167                                return
1168                            end
1169                            if win.mapped == 0 then
1170                                command = 0x10021
1171                            else
1172                                command = 0x10020
1173                            end
1174                            arg.attr = map.type
1175                            arg.name = map.target
1176                            arg.width = map.width
1177                            arg.height = map.height
1178                            arg.stride = map.stride
1179                            arg.format = map.format
1180                       else
1181                            if verbose > 0 then
1182                                print("### nothing to do")
1183                            end
1184                            return
1185                       end
1186
1187                       local msg = m:JSON({ command = command,
1188                                            appid   = win.appid,
1189                                            pid     = win.pid,
1190                                            arg     = arg
1191                       })
1192                       if verbose > 0 then
1193                           print("### <== sending " ..
1194                                 command_name(msg.command) ..
1195                                 " window message to '" .. win.name .. "'")
1196                           if verbose > 1 then
1197                               print(msg)
1198                           end
1199                       end
1200                       sc:send_message(homescreen, msg)
1201
1202                       if oper == 1 then -- create
1203                           local i = input_layer[win.layertype]
1204                           local p = self:application(win.appid)
1205                           local s = p.privileges.screen
1206
1207                           if s == "system" then
1208                               local a = animation({})
1209                               local r = m:JSON({surface = win.surface,
1210                                                 visible = 1,
1211                                                 raise   = 1})
1212                               self:window_request(r,a,0)
1213                           else
1214                               if i then
1215                                   if verbose > 0 then
1216                                       print("do not make resource for " ..
1217                                             "input window")
1218                                   end
1219                               else
1220                                   resclnt:resource_set_create("screen",
1221                                                                "driver",
1222                                                                win.appid,
1223                                                                win.surface)
1224                                   special_screen_sets[win.surface] = true
1225                               end
1226                           end
1227                       elseif oper == 2 then -- destroy
1228                           resclnt:resource_set_destroy("screen", win.surface)
1229                           special_screen_sets[win.surface] = nil
1230                       elseif oper == 6 then -- active
1231                           if win.active then
1232                               local i = input_layer[win.layertype]
1233                               local p = self:application(win.appid)
1234                               local s = p.privileges.screen
1235                               local surface = win.surface
1236                               if not i and s ~= "system" then
1237                                  resclnt:resource_set_acquire("screen",surface)
1238                                  resmgr:window_raise(win.appid, surface, 1)
1239                               end
1240                           end
1241                       end
1242                   end,
1243
1244   layer_update = function(self, oper, layer, mask)
1245                       if verbose > 0 then
1246                           print("### LAYER UPDATE:" ..
1247                                 layer_operation_name(oper) ..
1248                                 " mask: " .. tostring(mask))
1249                           if verbose > 1 then
1250                               print(layer)
1251                           end
1252                       end
1253                       if oper == 3 then -- visible
1254                          local command = 0x10008 -- change_layer
1255                          local msg = m:JSON({
1256                                          command = command,
1257                                          appid = "",
1258                                          arg = m:JSON({layer = layer.id,
1259                                                        visible = layer.visible
1260                                          })
1261                                 })
1262                          if verbose > 0 then
1263                             print("### <== sending "..command_name(command)..
1264                                   " layer message")
1265                             if verbose > 1 then
1266                                 print(msg)
1267                             end
1268                          end
1269                          sc:send_message(homescreen, msg)
1270                       else
1271                            if verbose > 0 then
1272                                print("### nothing to do")
1273                            end
1274                       end
1275                  end,
1276
1277   output_update = function(self, oper, out, mask)
1278                       local idx = out.index
1279                       local defidx = self.output_order[idx+1]
1280                       if verbose > 0 then
1281                           print("### OUTPUT UPDATE:" .. oper ..
1282                                 " mask: "..tostring(mask))
1283                       end
1284                       if not defidx then
1285                           return
1286                       end
1287                       print(out)
1288                       local outdef = self.outputs[defidx+1]
1289                       if (oper == 1) then -- create
1290                           if outdef then
1291                               self:output_request(m:JSON({index = idx,
1292                                                           id    = outdef.id,
1293                                                           name  = outdef.name
1294                                                           }))
1295                           end
1296                       elseif (oper == 5) then -- done
1297                           local ads = outdef.areas
1298                           local on = outdef.name
1299                           if ads then
1300                               for name,ad in pairs(ads) do
1301                                   local can = wmgr:canonical_name(on.."."..name)
1302                                   local a = m:JSON({name   = name,
1303                                                     output = out.index})
1304                                   for fld,val in pairs(ad) do
1305                                       a[fld] = self:geometry(out.width,
1306                                                              out.height,
1307                                                              val)
1308                                    end
1309                                    self:area_create(a)
1310                                    resmgr:area_create(area[can], outdef.zone)
1311                               end
1312                           end
1313                       end
1314                   end
1315 }
1316
1317
1318 imgr = input_manager {
1319   inputs = {{ name = "G27 Racing Wheel",
1320               id = 0,
1321               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1322                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1323                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1324                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1325              }}
1326   },
1327
1328   manager_update = function(self, oper)
1329                        if verbose > 0 then
1330                            print("### <== INPUT MANAGER UPDATE:" ..
1331                                  input_manager_operation_name(oper))
1332                        end
1333                    end,
1334
1335   input_update = function(self, oper, inp, mask)
1336                      if verbose > 0 then
1337                          print("### INPUT UPDATE:" ..
1338                                 input_operation_name(oper) ..
1339                                 " mask: " .. tostring(mask))
1340                           if verbose > 1 then
1341                               print(inp)
1342                           end
1343                       end
1344                  end,
1345   code_update  = function(self, oper, code, mask)
1346                      if verbose > 0 then
1347                          print("### CODE UPDATE: mask: " .. tostring(mask))
1348                          if verbose > 1 then
1349                              print(code)
1350                          end
1351                      end
1352                      local msg = m:JSON({ command = 1,
1353                                           appid = "org.tizen.ico.homescreen",
1354                                           arg = m:JSON({ device = code.device,
1355                                                          time = code.time,
1356                                                          input = code.input,
1357                                                          code = code.id,
1358                                                          state = code.state
1359                                            })
1360                      })
1361                      if verbose > 0 then
1362                          print("### <== sending " ..
1363                                command_name(msg.command) ..
1364                                " input message")
1365                          if verbose > 1 then
1366                              print(msg)
1367                          end
1368                      end
1369                      sc:send_message(homescreen, msg)
1370                  end
1371 }
1372
1373 sc = m:get_system_controller()
1374
1375 -- resource sets
1376 sets = {}
1377
1378 -- special screen resource sets
1379 -- TODO: just rewrite screen resource handling to use regular resource API
1380
1381 special_screen_sets = {}
1382
1383 -- user manager
1384 um = m:UserManager()
1385
1386 connected = false
1387 homescreen = ""
1388 onscreen = ""
1389
1390 -- these shoud be before wmgr:connect() is called
1391 if verbose > 0 then
1392    print("====== creating applications ======")
1393 end
1394 application {
1395     appid          = "default",
1396     area           = "Center.Full",
1397     privileges     = { screen = "none", audio = "none" },
1398     resource_class = "player",
1399     screen_priority = 0
1400 }
1401
1402 application {
1403     appid           = "weston",
1404     area            = "Center.Full",
1405     privileges      = { screen = "system", audio = "none" },
1406     resource_class  = "implicit",
1407     screen_priority = 30
1408 }
1409
1410 application {
1411     appid           = "org.tizen.ico.homescreen",
1412     area            = "Center.Full",
1413     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1414     privileges      = { screen = "system", audio = "system" },
1415     resource_class  = "player",
1416     screen_priority = 20
1417 }
1418
1419 application {
1420     appid           = "org.tizen.ico.statusbar",
1421     area            = "Center.Status",
1422     privileges      = { screen = "system", audio = "none" },
1423     resource_class  = "player",
1424     screen_priority = 20
1425 }
1426
1427 application {
1428     appid           = "org.tizen.ico.onscreen",
1429     area            = "Center.Full",
1430     privileges      = { screen = "system", audio = "system" },
1431     resource_class  = "player",
1432     screen_priority = 20
1433 }
1434
1435 application {
1436     appid           = "org.tizen.ico.login",
1437     area            = "Center.Full",
1438     privileges      = { screen = "system", audio = "system" },
1439     resource_class  = "player",
1440     screen_priority = 20
1441 }
1442
1443 application {
1444     appid           = "org.tizen.ico.camera_left",
1445     area            = "Center.SysApp.Left",
1446     privileges      = { screen = "system", audio = "none" },
1447     requisites      = { screen = "blinker_left", audio = "none" },
1448     resource_class  = "player",
1449     screen_priority = 30
1450 }
1451
1452 application {
1453     appid           = "org.tizen.ico.camera_right",
1454     area            = "Center.SysApp.Right",
1455     privileges      = { screen = "system", audio = "none" },
1456     requisites      = { screen = "blinker_right", audio = "none" },
1457     resource_class  = "player",
1458     screen_priority = 30
1459 }
1460
1461 application {
1462     appid           = "net.zmap.navi",
1463     area            = "Center.Full",
1464     privileges      = { screen = "none", audio = "none" },
1465     resource_class  = "navigator",
1466     screen_priority = 30
1467 }
1468
1469 application {
1470     appid           = "GV3ySIINq7.GhostCluster",
1471     area            = "Center.Full",
1472     privileges      = { screen = "none", audio = "none" },
1473     resource_class  = "system",
1474     screen_priority = 30
1475 }
1476
1477 if sc then
1478     sc.client_handler = function (self, cid, msg)
1479         local command = msg.command
1480         local appid = msg.appid
1481         if verbose > 0 then
1482             print('### ==> client handler:')
1483             if verbose > 1 then
1484                 print(msg)
1485             end
1486         end
1487
1488         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1489         -- disconnection
1490
1491         if command == 0xFFFF then
1492             if verbose > 1 then
1493                 print('client ' .. cid .. ' disconnected')
1494             end
1495             if msg.appid == homescreen then
1496                 homescreen = ""
1497                 for i,v in pairs(special_screen_sets) do
1498                     resclnt:resource_set_destroy("screen", i)
1499                     special_screen_sets[i] = nil
1500                 end
1501             end
1502             return
1503         end
1504
1505         -- handle the connection to weston
1506
1507         if appid then
1508             if appid == "org.tizen.ico.homescreen" then
1509                 print('Setting homescreen='..appid)
1510                 homescreen = appid
1511                 if command and command == 1 then
1512                     send_driving_mode_to_home_screen()
1513                     send_night_mode_to_home_screen()
1514                 end
1515             elseif appid == "org.tizen.ico.onscreen" then
1516                 onscreen = appid
1517             end
1518
1519             if not connected and appid == "org.tizen.ico.homescreen" then
1520                 print('Trying to connect to weston...')
1521                 connected = wmgr:connect()
1522             end
1523         end
1524     end
1525
1526     sc.generic_handler = function (self, cid, msg)
1527         if verbose > 0 then
1528             print('### ==> generic handler:')
1529             if verbose > 1 then
1530                print(msg)
1531             end
1532         end
1533     end
1534
1535     sc.window_handler = function (self, cid, msg)
1536         if verbose > 0 then
1537             print('### ==> received ' ..
1538                    command_name(msg.command) .. ' message')
1539             if verbose > 1 then
1540                 print(tostring(msg))
1541             end
1542         end
1543
1544         local a = animation({})
1545         local nores = false
1546         if msg.command == 0x10003 then       -- ico SHOW command
1547             local raise_mask = 0x01000000
1548             local lower_mask = 0x02000000
1549             local nores_mask = 0x40000000
1550             local time_mask  = 0x00ffffff
1551             msg.arg.visible = 1
1552             if msg.arg then
1553                 local time = 200
1554                 if  msg.arg.anim_time then
1555                     local t = msg.arg.anim_time
1556                     time = m:AND(t, time_mask)
1557                     nores = not m:AND(t, nores_mask)
1558                     if m:AND(t, raise_mask) then
1559                         msg.arg.raise = 1
1560                     elseif m:AND(t, lower_mask) then
1561                         msg.arg.raise = 0
1562                     end
1563                 end
1564                 if msg.arg.anim_name then
1565                     a.show = { msg.arg.anim_name, time }
1566                     print('time: ' .. tostring(a.show[2]))
1567                 end
1568             end
1569             if not nores then
1570                 local p = wmgr:application(msg.appid)
1571                 local s = p.privileges.screen
1572                 if s == "system" then
1573                     nores = true
1574                     if not msg.arg.raise then
1575                         msg.arg.raise = 1
1576                     end
1577                 end
1578             end
1579             if verbose > 2 then
1580                 print('### ==> SHOW')
1581                 print(tostring(msg.arg))
1582             end
1583             if nores then
1584                 wmgr:window_request(msg.arg, a, 0)
1585             else
1586                 local surface = msg.arg.surface
1587                 resclnt:resource_set_acquire("screen", surface)
1588                 resmgr:window_raise(msg.appid, surface, 1)
1589             end
1590         elseif msg.command == 0x10004 then   -- ico HIDE command
1591             local raise_mask = 0x01000000
1592             local lower_mask = 0x02000000
1593             local nores_mask = 0x40000000
1594             local time_mask  = 0x00ffffff
1595             msg.arg.visible = 0
1596             if msg.arg then
1597                 local time = 200
1598                 if msg.arg.anim_time then
1599                     local t = msg.arg.anim_time
1600                     time = m:AND(t, time_mask)
1601                     nores = not m:AND(t, nores_mask)
1602                 end
1603                 if msg.arg.anim_name then
1604                     a.hide = { msg.arg.anim_name, time }
1605                     print('hide animation time: ' .. tostring(a.hide[2]))
1606                 end
1607             end
1608             if not nores then
1609                 local p = wmgr:application(msg.appid)
1610                 local s = p.privileges.screen
1611                 if s == "system" then
1612                     nores = true
1613                     msg.arg.raise = 0
1614                 end
1615             end
1616             if verbose > 2 then
1617                 print('### ==> HIDE REQUEST')
1618                 print(tostring(msg.arg))
1619             end
1620             if nores then
1621                 wmgr:window_request(msg.arg, a, 0)
1622             else
1623                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1624             end
1625         elseif msg.command == 0x10005 then   -- ico MOVE
1626             if verbose > 2 then
1627                 print('### ==> MOVE REQUEST')
1628                 print(tostring(msg.arg))
1629             end
1630             if msg.arg.zone then
1631                 msg.arg.area = msg.arg.zone
1632             end
1633             wmgr:window_request(msg.arg, a, 0)
1634             -- TODO: handle if area changed
1635         elseif msg.command == 0x10007 then   -- ico CHANGE_ACTIVE
1636             if not msg.arg.active then
1637                 msg.arg.active = 3 -- pointer + keyboard
1638             end
1639             if verbose > 2 then
1640                 print('### ==> CHANGE_ACTIVE REQUEST')
1641                 print(tostring(msg.arg))
1642             end
1643             wmgr:window_request(msg.arg, a, 0)
1644         elseif msg.command == 0x10008 then   -- ico CHANGE_LAYER
1645             if verbose > 2 then
1646                 print('### ==> CHANGE_LAYER REQUEST')
1647                 print(tostring(msg.arg))
1648             end
1649             --[[
1650             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1651                 print("do not change layer for other than cursor or touch")
1652                 return
1653             end
1654             --]]
1655             wmgr:window_request(msg.arg, a, 0)
1656         elseif msg.command == 0x10020 then   -- ico MAP_THUMB
1657             local framerate = msg.arg.framerate
1658             if not framerate or framerate < 0 then
1659                 framerate = 0
1660             end
1661             msg.arg.mapped = 1
1662             if verbose > 2 then
1663                 print('### ==> MAP_THUMB REQUEST')
1664                 print(msg.arg)
1665                 print('framerate: '..framerate)
1666             end
1667             wmgr:window_request(msg.arg, a, framerate)
1668         elseif msg.command == 0x10021 then   -- ico UNMAP_THUMB
1669             msg.arg.mapped = 0
1670             if verbose > 2 then
1671                 print('### ==> UNMAP_THUMB REQUEST')
1672                 print(msg.arg)
1673             end
1674             wmgr:window_request(msg.arg, a, 0)
1675 --[[
1676         elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
1677             local shmname = msg.arg.anim_name
1678             local bufsize = msg.arg.width
1679             local bufnum  = msg.arg.height
1680             if shmname and bufsize and bufnum then
1681                 if verbose > 2 then
1682                     print('### ==> MAP_BUFFER REQUEST')
1683                     print("shmaname='" .. shmname ..
1684                           "' bufsize='" .. bufsize ..
1685                           " bufnum=" .. bufnum)
1686                 end
1687                 wmgr:buffer_request(shmname, bufsize, bufnum)
1688             end
1689 --]]
1690         elseif msg.command == 0x10030 then   -- ico SHOW_LAYER command
1691             msg.arg.visible = 1
1692             if verbose > 2 then
1693                 print('### ==> SHOW_LAYER REQUEST')
1694                 print(msg.arg)
1695             end
1696             wmgr:layer_request(msg.arg)
1697         elseif msg.command == 0x10031 then   -- ico HIDE_LAYER command
1698             msg.arg.visible = 0
1699             if verbose > 2 then
1700                 print('### ==> HIDE_LAYER REQUEST')
1701                 print(msg.arg)
1702             end
1703             wmgr:layer_request(msg.arg)
1704         end
1705     end
1706
1707     sc.input_handler = function (self, cid, msg)
1708         if verbose > 0 then
1709             print('### ==> input handler: ' .. command_name(msg.command))
1710             if verbose > 1 then
1711                 print(msg)
1712             end
1713         end
1714         if msg.command == 0x20001 then -- add_input
1715             msg.arg.appid = msg.appid
1716             if verbose > 2 then
1717                 print('### ==> ADD_INPUT REQUEST')
1718                 print(tostring(msg.arg))
1719             end
1720             imgr:input_request(msg.arg)
1721         elseif msg.command == 0x20002 then -- del_input
1722             msg.arg.appid = ''
1723             if verbose > 2 then
1724                 print('### ==> DEL_INPUT REQUEST')
1725                 print(tostring(msg.arg))
1726             end
1727             imgr:input_request(msg.arg)
1728         -- elseif msg.command == 0x20003 then -- send_input
1729         end
1730     end
1731
1732     sc.user_handler = function (self, cid, msg)
1733         if verbose > 0 then
1734             print('### ==> user handler: ' .. command_name(msg.command))
1735             if verbose > 1 then
1736                 print(msg)
1737             end
1738         end
1739
1740         if not um then
1741             print("User Manager not initialized")
1742             return
1743         end
1744
1745         if msg.command == 0x30001 then -- change_user
1746             print("command CHANGE_USER")
1747             if not msg.arg then
1748                 print("invalid message")
1749                 return
1750             end
1751
1752             username = msg.arg.user
1753             passwd = msg.arg.pass
1754
1755             if not username then
1756                 username = ""
1757             end
1758
1759             if not passwd then
1760                 passwd = ""
1761             end
1762
1763             success = um:changeUser(username, passwd)
1764
1765             if not success then
1766                 reply = m.JSON({
1767                     appid = msg.appid,
1768                     command = cmd
1769                 })
1770                 if sc:send_message(msg.appid, reply) then
1771                     print('*** sent authentication failed message')
1772                 else
1773                     print('*** failed to send authentication failed message')
1774                 end
1775             end
1776
1777         elseif msg.command == 0x30002 then -- get_userlist
1778             print("command GET_USERLIST")
1779             if not msg.appid then
1780                 print("invalid message")
1781                 return
1782             end
1783
1784             users, currentUser = um:getUserList()
1785
1786             if not users then
1787                 print("failed to get user list")
1788                 return
1789             end
1790
1791             nUsers = 0
1792
1793             for i,v in pairs(users) do
1794                 nUsers = nUsers + 1
1795             end
1796
1797             if not currentUser then
1798                 currentUser = ""
1799             end
1800
1801             if verbose > 1 then
1802                 print("current user: " .. currentUser)
1803                 print("user list:")
1804                 for i,v in pairs(users) do
1805                     print(v)
1806                 end
1807             end
1808
1809             reply = m.JSON({
1810                 appid = msg.appid,
1811                 command = cmd,
1812                 arg = m.JSON({
1813                     user_num = nUsers,
1814                     user_list = users,
1815                     user_login = currentUser
1816                 })
1817             })
1818
1819             if verbose > 1 then
1820                 print("### <== GetUserList reply: " .. tostring(reply))
1821             end
1822
1823             if sc:send_message(msg.appid, reply) then
1824                 print('*** reply OK')
1825             else
1826                 print('*** reply FAILED')
1827             end
1828
1829         elseif msg.command == 0x30003 then -- get_lastinfo
1830             print("command GET_LASTINFO")
1831             if not msg.appid then
1832                 print("invalid message")
1833                 return
1834             end
1835
1836             lastInfo = um:getLastInfo(msg.appid)
1837
1838             if not lastInfo then
1839                 print("failed to get last info for app" .. msg.appid)
1840                 return
1841             end
1842
1843             reply = m.JSON({
1844                 appid = msg.appid,
1845                 command = cmd,
1846                 arg = m.JSON({
1847                     lastinfo = lastinfo
1848                 })
1849             })
1850
1851             if sc:send_message(msg.appid, reply) then
1852                 print('*** reply OK')
1853             else
1854                 print('*** reply FAILED')
1855             end
1856
1857         elseif msg.command == 0x30004 then -- set_lastinfo
1858             print("command SET_LASTINFO")
1859             if not msg.arg or not msg.appid then
1860                 print("invalid message")
1861                 return
1862             end
1863
1864             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1865         end
1866     end
1867
1868     sc.resource_handler = function (self, cid, msg)
1869         if verbose > 0 then
1870             print('### ==> resource handler: ' .. command_name(msg.command))
1871             if verbose > 1 then
1872                 print(msg)
1873             end
1874         end
1875
1876         createResourceSet = function (ctl, client, msg)
1877             cb = function(rset, data)
1878                 print("> resource callback")
1879
1880                 -- type is either basic (0) or interrupt (1)
1881                 requestType = 0
1882                 if msg.res.type then
1883                     requestType = msg.res.type
1884                 end
1885
1886                 if rset.acquired then
1887                     cmd = 0x00040001 -- acquire
1888                 else
1889                     cmd = 0x00040002 -- release
1890                 end
1891
1892                 reply = m.JSON({
1893                         appid = data.client,
1894                         command = cmd,
1895                         res = {
1896                             type = requestType
1897                         }
1898                     })
1899
1900                 if rset.resources.audio_playback then
1901                     reply.res.sound = {
1902                         zone = "driver",
1903                         name = msg.appid,
1904                         adjust = 0,
1905                         -- id = "0"
1906                     }
1907                 end
1908
1909                 if rset.resources.display then
1910                     reply.res.window = {
1911                         zone = "driver",
1912                         name = msg.appid,
1913                         -- id = "0"
1914                     }
1915                 end
1916
1917                 if rset.resources.input then
1918                     reply.res.input = {
1919                         name = msg.appid,
1920                         event = 0
1921                     }
1922                 end
1923                 print("sending message to client: " .. data.client)
1924
1925                 if sc:send_message(data.client, reply) then
1926                     print('*** reply OK')
1927                 else
1928                     print('*** reply FAILED')
1929                 end
1930             end
1931
1932             rset = m:ResourceSet({
1933                     application_class = "player",
1934                     zone = "driver", -- msg.zone ("full")
1935                     callback = cb
1936                 })
1937
1938             rset.data = {
1939                 cid = cid,
1940                 ctl = ctl
1941             }
1942
1943             if msg.res.sound then
1944                 rset:addResource({
1945                         resource_name = "audio_playback"
1946                     })
1947                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1948                 rset.resources.audio_playback.attributes.appid = msg.appid
1949                 print("sound name: " .. msg.res.sound.name)
1950                 print("sound zone:" .. msg.res.sound.zone)
1951                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1952                 if msg.res.sound.id then
1953                     print("sound id: " .. msg.res.sound.id)
1954                 end
1955             end
1956
1957             if msg.res.input then
1958                 rset:addResource({
1959                         resource_name = "input"
1960                     })
1961                 rset.resources.input.attributes.pid = tostring(msg.pid)
1962                 rset.resources.input.attributes.appid = msg.appid
1963                 print("input name: " .. msg.res.sound.name)
1964                 print("input event:" .. tostring(msg.res.input.event))
1965             end
1966
1967             if msg.res.window then
1968                 rset:addResource({
1969                         resource_name = "display"
1970                     })
1971                 rset.resources.display.attributes.pid = tostring(msg.pid)
1972                 rset.resources.display.attributes.appid = msg.appid
1973                 print("display name: " .. msg.res.display.name)
1974                 print("display zone:" .. msg.res.display.zone)
1975                 if msg.res.display.id then
1976                     print("display id: " .. msg.res.display.id)
1977                 end
1978             end
1979
1980             return rset
1981         end
1982
1983         -- parse the message
1984
1985         -- fields common to all messages:
1986         --      msg.command
1987         --      msg.appid
1988         --      msg.pid
1989
1990         if msg.command == 0x40011 then -- create_res
1991             print("command CREATE_RES")
1992
1993             if not sets.cid then
1994                 sets.cid = createResourceSet(self, cid, msg)
1995             end
1996
1997         elseif msg.command == 0x40012 then -- destroy_res
1998             print("command DESTROY_RES")
1999
2000             if sets.cid then
2001                 sets.cid:release()
2002             end
2003
2004             sets.cid = nil -- garbage collecting
2005
2006         elseif msg.command == 0x40001 then -- acquire_res
2007             print("command ACQUIRE_RES")
2008
2009             if not sets.cid then
2010                 sets.cid = createResourceSet(self, cid, msg)
2011             end
2012
2013             sets.cid:acquire()
2014
2015         elseif msg.command == 0x40002 then -- release_res
2016             print("command RELEASE_RES")
2017
2018             if sets.cid then
2019                 sets.cid:release()
2020             end
2021
2022         elseif msg.command == 0x40003 then -- deprive_res
2023             print("command DEPRIVE_RES")
2024
2025         elseif msg.command == 0x40004 then -- waiting_res
2026             print("command WAITING_RES")
2027
2028         elseif msg.command == 0x40005 then -- revert_res
2029             print("command REVERT_RES")
2030         end
2031     end
2032
2033     sc.inputdev_handler = function (self, cid, msg)
2034         if verbose > 0 then
2035             print('*** inputdev handler: ' .. command_name(msg.command))
2036             if verbose > 1 then
2037                 print(msg)
2038             end
2039         end
2040     end
2041 end
2042
2043 function send_driving_mode_to_home_screen()
2044     if homescreen == "" then
2045         return
2046     end
2047
2048     local driving_mode = mdb.select.select_driving_mode.single_value
2049
2050     if not driving_mode then driving_mode = 0 end
2051
2052     local reply = m:JSON({ command = 0x60001,
2053                            arg     = m:JSON({ stateid = 1,
2054                                               state   = driving_mode
2055                                      })
2056                   })
2057
2058     if verbose > 0 then
2059         print("### <== sending " .. command_name(reply.command) .. " message")
2060         if verbose > 1 then
2061             print(reply)
2062         end
2063     end
2064
2065     sc:send_message(homescreen, reply)
2066 end
2067
2068 function send_night_mode_to_home_screen()
2069     if homescreen == "" then
2070         return
2071     end
2072
2073     local night_mode = mdb.select.select_night_mode.single_value
2074
2075     if not night_mode then night_mode = 0 end
2076
2077     local reply = m:JSON({ command = 0x60001,
2078                            arg     = m:JSON({ stateid = 2,
2079                                               state   = night_mode
2080                                      })
2081                   })
2082
2083     if verbose > 0 then
2084         print("### <== sending " .. command_name(reply.command) .. " message")
2085         if verbose > 1 then
2086             print(reply)
2087         end
2088      end
2089
2090      sc:send_message(homescreen, reply)
2091 end
2092
2093 -- we should have 'audio_playback' defined by now
2094 m:try_load_plugin('telephony')